package com.nantong.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nantong.admin.annotation.PermissionRegister;
import com.nantong.admin.domain.Product;
import com.nantong.admin.domain.model.request.product.ProductSearch;
import com.nantong.admin.domain.model.response.product.ProductVo;
import com.nantong.admin.service.IProductService;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;
import com.nantong.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品Controller
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/product")
@PermissionRegister(
    name = "产品管理",
    key = "system:product",
    type = 2,  // 2=菜单
    path = "product",
    component = "/views/Product/ProductView.vue",
    icon = "shop",
    sortOrder = 3,
    parentKey = "system"  // 父菜单是system
)
@Tag(name = "产品管理", description = "产品管理相关接口")
public class ProductController {

    private final IProductService productService;

    /**
     * 获取产品列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取产品列表", description = "获取系统产品列表")
    @SaCheckPermission("system:product:list")
    @PermissionRegister(
            name = "产品列表查询",
            key = "system:product:list",
            type = 3,  // 3=按钮
            remark = "查询产品数据列表"
    )
    public R<List<ProductVo>> list(ProductSearch productSearch) {
        List<ProductVo> products = productService.searchList(productSearch);
        return R.ok(products);
    }

    /**
     * 获取产品分页
     */
    @SaCheckPermission("system:product:page")
    @GetMapping("/page")
    @Operation(summary = "产品分页查询", description = "获取系统产品分页数据")
    @PermissionRegister(
            name = "产品分页查询",
            key = "system:product:page",
            type = 3,  // 3=按钮
            remark = "查询产品数据分页"
    )
    public TableDataInfo<ProductVo> page(ProductSearch productSearch, PageQuery pageQuery) {
        return productService.selectPageList(productSearch, pageQuery);
    }

    /**
     * 获取产品详细信息
     */
    @GetMapping("/{productId}")
    @Operation(summary = "获取产品详情", description = "根据产品ID获取产品详情")
    @SaCheckPermission("system:product:query")
    @PermissionRegister(
            name = "产品详情查询",
            key = "system:product:query",
            type = 3,  // 3=按钮
            remark = "查询产品数据详情"
    )
    public R<Product> getInfo(@PathVariable Long productId) {
        Product product = productService.getById(productId);
        return R.ok(product);
    }

    /**
     * 新增产品
     */
    @PostMapping
    @Operation(summary = "新增产品", description = "新增系统产品")
    @SaCheckPermission("system:product:add")
    @PermissionRegister(
            name = "产品新增",
            key = "system:product:add",
            type = 3,  // 3=按钮
            remark = "新增产品数据"
    )
    public R<Void> add(@RequestBody Product product) {
        productService.save(product);
        return R.ok();
    }

    /**
     * 修改产品
     */
    @PutMapping
    @Operation(summary = "修改产品", description = "修改系统产品")
    @SaCheckPermission("system:product:edit")
    @PermissionRegister(
            name = "产品修改",
            key = "system:product:edit",
            type = 3,  // 3=按钮
            remark = "修改产品数据"
    )
    public R<Void> edit(@RequestBody Product product) {
        productService.updateById(product);
        return R.ok();
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/{productId}")
    @Operation(summary = "删除产品", description = "删除系统产品")
    @SaCheckPermission("system:product:delete")
    @PermissionRegister(
            name = "产品删除",
            key = "system:product:delete",
            type = 3,  // 3=按钮
            remark = "删除产品数据"
    )
    public R<Void> remove(@PathVariable Long productId) {
        productService.removeById(productId);
        return R.ok();
    }
}